package cn.study.luckyinfrastructure.gateway.iml;

import cm.study.config.enums.LdExeceptionEnum;
import cm.study.config.util.AssertUtil;
import cn.study.luckyclient.dto.query.ActivityListByParamQuery;
import cn.study.luckydomain.activity.ActivityEntity;
import cn.study.luckydomain.activityrule.ActivityRuleEntity;
import cn.study.luckydomain.gateway.ActivityGateway;
import cn.study.luckyinfrastructure.convertor.ActivityConvertor;
import cn.study.luckyinfrastructure.gateway.iml.dataObject.ActivityDB;
import cn.study.luckyinfrastructure.gateway.iml.mapper.ActivityMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;
@Slf4j
@AllArgsConstructor
@Component
public class ActivityGatewayImpl implements ActivityGateway {
    private final ActivityMapper activityMapper;

    @Override
    public ActivityEntity save(ActivityEntity activityEntity) {
        ActivityDB activityDB = ActivityConvertor.toActivityDB(activityEntity);
        if(Objects.isNull(activityEntity.getId())){
            int insert = activityMapper.insert(activityDB);
            AssertUtil.isTrue(insert<1, LdExeceptionEnum.ADD_ERROR.getDescription());
        }
        else{
            int i = activityMapper.updateById(activityDB);
            AssertUtil.isTrue(i<1,LdExeceptionEnum.UPDATE_ERROR.getDescription());
        }
        log.info("欣喜1"+ActivityConvertor.toEntity(activityDB));
        return ActivityConvertor.toEntity(activityDB);
    }

    @Override
    public IPage<ActivityEntity> page(ActivityListByParamQuery query) {
      IPage<ActivityDB> page=activityMapper.page(new Page<ActivityDB>(query.getPageIndex(), query.getPageSize()),query);
      return page.convert(ActivityConvertor::toEntity);
    }
}
